package com.ljl.ssm.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ljl.ssm.entity.Product;
import com.ljl.ssm.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author BaoZhou
 * @since 2019-07-07
 */
@Controller
public class ProductController {

    @Autowired
    IProductService iProductService;

    @RequestMapping(value = "initPrd",method = RequestMethod.GET)
    public String doinit( @RequestParam(defaultValue = "1",value = "pn")Integer pn,
                          @RequestParam(defaultValue = "mi",value="item") String item,
                          ModelMap map){

       // PageHelper.startPage(pn,10);
        PageInfo pageInfoT= iProductService.selectAll();
        map.put("pageInfoT", pageInfoT);
       // List<Product> products= iProductService.selectList(item);
        System.out.println( item );
        //PageInfo pageInfo = new PageInfo<>(products);
        PageInfo pageInfo=iProductService.selectList(pn,item);
        map.put("pageInfo", pageInfo);
        map.put("item", item);
        return "liebiao";
    }


    @RequestMapping("searchproduct")
    public String doSearch(@RequestParam String shuru,
                           @RequestParam(required = false,defaultValue = "1",value = "pn")Integer pn,
                           ModelMap map)  {

        PageInfo<Product> pageInfo= iProductService.selectListLike(pn,shuru);
        //使用PageInfo包装查询结果，只需要将pageInfo交给页面就可以
        PageInfo pageInfoT= iProductService.selectAll();
        map.put("pageInfoT", pageInfoT);
        System.out.println(shuru);
        map.put("pageInfo", pageInfo);
        map.put("shuru", shuru);


        return "liebiao";
    }





}

